<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Convert a non-executable tar/zip archive to an executable phar archive</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="phardata.converttodata.html">PharData::convertToData</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="phardata.copy.html">PharData::copy</a></div>
 <div class="up"><a href="class.phardata.html">PharData</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="phardata.converttoexecutable" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PharData::convertToExecutable</h1>
  <p class="verinfo">(PHP &gt;= 5.3.0, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">PharData::convertToExecutable</span> &mdash; <span class="dc-title">Convert a non-executable tar/zip archive to an executable phar archive</span></p>

 </div>


 <div class="refsect1 description" id="refsect1-phardata.converttoexecutable-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.phar.html" class="type Phar">Phar</a></span> <span class="methodname"><strong>PharData::convertToExecutable</strong></span>
    ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$format</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$compression</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$extension</code></span>
  ]]] )</div>

  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">此方法需要
将 <var class="filename">php.ini</var> 中的 <em>phar.readonly</em> 设为
<em>0</em> 以适合 <a href="class.phar.html" class="classname">Phar</a>
对象.  否则, 将抛出<a href="class.pharexception.html" class="classname">PharException</a>.</p></p></blockquote>
  <p class="para">
   This method is used to convert a non-executable tar or zip archive to an
   executable phar archive.  Any of the three executable file formats
   (phar, tar or zip) can be used, and whole-archive compression can also be performed.
  </p>
  <p class="para">
   If no changes are specified, this method throws a <a href="class.badmethodcallexception.html" class="classname">BadMethodCallException</a>.
  </p>
  <p class="para">
   If successful, the method creates a new archive on disk and returns a <a href="class.phar.html" class="classname">Phar</a>
   object.  The old archive is not removed from disk, and should be done manually after
   the process has finished.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-phardata.converttoexecutable-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">format</code></em></dt>

     <dd>

      <p class="para">
       This should be one of <em>Phar::PHAR</em>, <em>Phar::TAR</em>,
       or <em>Phar::ZIP</em>.  If set to <strong><code>NULL</code></strong>, the existing file format
       will be preserved.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">compression</code></em></dt>

     <dd>

      <p class="para">
       This should be one of <em>Phar::NONE</em> for no whole-archive
       compression, <em>Phar::GZ</em> for zlib-based compression, and
       <em>Phar::BZ2</em> for bzip-based compression.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">extension</code></em></dt>

     <dd>

      <p class="para">
       This parameter is used to override the default file extension for a
       converted archive.  Note that all zip- and tar-based phar archives must contain
       <em>.phar</em> in their file extension in order to be processed as a
       phar archive.
      </p>
      <p class="para">
       If converting to a phar-based archive, the default extensions are
       <em>.phar</em>, <em>.phar.gz</em>, or <em>.phar.bz2</em>
       depending on the specified compression.  For tar-based phar archives, the
       default extensions are <em>.phar.tar</em>, <em>.phar.tar.gz</em>,
       and <em>.phar.tar.bz2</em>.  For zip-based phar archives, the
       default extension is <em>.phar.zip</em>.
      </p>
     </dd>

    
   </dl>

  </p>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-phardata.converttoexecutable-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The method returns a <a href="class.phar.html" class="classname">Phar</a> object on success and throws an
   exception on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-phardata.converttoexecutable-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   This method throws <a href="class.badmethodcallexception.html" class="classname">BadMethodCallException</a> when unable
   to compress, an unknown compression method has been specified, the requested
   archive is buffering with <span class="function"><a href="phar.startbuffering.html" class="function">Phar::startBuffering()</a></span> and
   has not concluded with <span class="function"><a href="phar.stopbuffering.html" class="function">Phar::stopBuffering()</a></span>, an
   <a href="class.unexpectedvalueexception.html" class="classname">UnexpectedValueException</a> if write support is disabled,
   and a <a href="class.pharexception.html" class="classname">PharException</a> if any problems are encountered
   during the phar creation process.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-phardata.converttoexecutable-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-689">
    <p><strong>Example #1 A <span class="function"><strong>PharData::convertToExecutable()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     Using PharData::convertToExecutable():
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tarphar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PharData</span><span style="color: #007700">(</span><span style="color: #DD0000">'myphar.tar'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;convert&nbsp;it&nbsp;to&nbsp;the&nbsp;phar&nbsp;file&nbsp;format<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;note&nbsp;that&nbsp;myphar.tar&nbsp;is&nbsp;*not*&nbsp;unlinked<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$tarphar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">convertToExecutable</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">PHAR</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;creates&nbsp;myphar.phar<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createDefaultStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'cli.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'web/index.php'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;creates&nbsp;myphar.phar.tgz<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$compressed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$tarphar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">convertToExecutable</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">TAR</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">GZ</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'.phar.tgz'</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;handle&nbsp;the&nbsp;error&nbsp;here<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-phardata.converttoexecutable-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="phar.converttoexecutable.html" class="function" rel="rdfs-seeAlso">Phar::convertToExecutable()</a> - Convert a phar archive to another executable phar archive file format</span></li>
    <li class="member"><span class="function"><a href="phar.converttodata.html" class="function" rel="rdfs-seeAlso">Phar::convertToData()</a> - Convert a phar archive to a non-executable tar or zip file</span></li>
    <li class="member"><span class="function"><a href="phardata.converttodata.html" class="function" rel="rdfs-seeAlso">PharData::convertToData()</a> - Convert a phar archive to a non-executable tar or zip file</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="phardata.converttodata.html">PharData::convertToData</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="phardata.copy.html">PharData::copy</a></div>
 <div class="up"><a href="class.phardata.html">PharData</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
